package com.wzcl.app.model.vo;

import com.wzcl.app.dao.model.Course;
import com.wzcl.app.dao.model.CourseTeacher;
import com.wzcl.app.model.common.WZCL;
import com.wzcl.app.model.dto.CourseTeacherDto;
import com.wzcl.app.service.CourseTeacherService;
import com.wzcl.app.service.impl.CourseTeacherServiceImpl;
import com.wzcl.app.utils.SpringUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = false)
@Data
public class CourseVo extends BaseVo {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "名称")
    private String name;

    @ApiModelProperty(value = "难度")
    private Integer hard;
    private String hardStr;

    @ApiModelProperty(value = "描述")
    private String description;

    @ApiModelProperty(value = "是否含有资料")
    private Boolean haveBook;

    @ApiModelProperty(value = "是否有直播")
    private Boolean haveLiveRoom;

    @ApiModelProperty(value = "课程状态")
    private Integer courseState;
    private String courseStateStr;

    @ApiModelProperty(value = "课时数")
    private Integer count;

    @ApiModelProperty(value = "课程类型")
    private Integer courseType;
    private String courseTypeStr;

    @ApiModelProperty(value = "开始课程时间")
    private Date startDate;

    @ApiModelProperty(value = "结束课程时间")
    private Date endDate;

    @ApiModelProperty(value = "每天开始时间")
    private Date startTime;

    @ApiModelProperty(value = "每天结束时间")
    private Date endTime;

    @ApiModelProperty(value = "开始时间的前缀")
    private String timePrefix;

    @ApiModelProperty(value = "课程图片")
    private List<String> pictures;

    @ApiModelProperty(value = "课程视频")
    private List<String> videos;

    @ApiModelProperty(value = "海报")
    private String posters;

    @ApiModelProperty(value = "状态")
    private Integer state;
    private String stateStr;

    @ApiModelProperty(value = "商品信息")
    private CommodityVo commodity;

    @ApiModelProperty(value = "虚拟观看数")
    private Long viewVolume;

    @ApiModelProperty(value = "实际观看数")
    private Long realViewVolume;

    @ApiModelProperty(value = "虚拟销售数")
    private Integer saleVolume;

    @ApiModelProperty(value = "实际销售数")
    private Integer realSaleVolume;

    //-------关联属性--------
    private Integer gradeId;
    private String  gradeName;

    private Integer subjectId;
    private String  subjectName;

    @ApiModelProperty(value = "老师")
    private List<CourseTeacherVo> courseTeacher;

    private CourseVo() {
    }

    public static CourseVo create(Course course){
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(course,courseVo,"commodity","pictures","videos");
        if(course.getGrade() != null){
            courseVo.gradeId = course.getGrade().getId();
            courseVo.gradeName = course.getGrade().getName();
        }
        if(course.getSubject() != null){
            courseVo.subjectId = course.getSubject().getId();
            courseVo.subjectName = course.getSubject().getName();
        }
        if(course.getCommodity()!=null){
            courseVo.commodity = CommodityVo.create(course.getCommodity());
        }
        if(StringUtils.isNotBlank(course.getPictures())){
            courseVo.pictures = Arrays.asList(course.getPictures().split(","));
        }else{
            ArrayList<String> list = new ArrayList<>();
            list.add(DEFAULT_IMAGE);
            courseVo.pictures = list;
        }
        courseVo.translationValueStr();
        courseVo.processResources();
        return courseVo;
    }

    @Override
    public void translationValueStr() {
        Map<String, Map<Integer, String>> map = getTranslationMap();
        this.hardStr = map.get(WZCL.GROUP_NAME.HARD_LEVEL).get(this.hard);
        this.courseStateStr = map.get(WZCL.GROUP_NAME.COURSE_STATE).get(this.courseState);
        this.courseTypeStr = map.get(WZCL.GROUP_NAME.COURSE_TYPE).get(this.courseType);
        this.stateStr = map.get(WZCL.GROUP_NAME.STATE).get(this.state);
    }

    @Override
    public void processResources() {
        try {
            CourseTeacherService CourseTeacherService = (CourseTeacherServiceImpl) SpringUtil.getBean("courseTeacherServiceImpl");
            CourseTeacherDto courseTeacherDto = new CourseTeacherDto();
            courseTeacherDto.setCourseId(this.getId());
            List<CourseTeacher> courseTeacherList = CourseTeacherService.getCourseTeacherList(courseTeacherDto);
            if(courseTeacherList != null && courseTeacherList.size()>0){
                this.courseTeacher = courseTeacherList.stream().map(CourseTeacherVo::create).collect(Collectors.toList());
            }
        }catch (Exception e){

        }
    }
}
